
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cmd_obj_ext.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "acaccountao_adaptor.h"
#include "acaccountao_req_resp.h"


std::string g_sacaccountaoStandAlone;

///////////////////////
// class CACAccountAoSvcFactory
int CACAccountAoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
    pCntl->getConfig("StandAlone", g_sacaccountaoStandAlone);
         

	return CACAccountAo::Initialize(pCntl);
}

int CACAccountAoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CACAccountAoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CACAccountAoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CACAccountAoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CACAccountAoService(dwcmdId));
}

void CACAccountAoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[ACACCOUNTAO_ADDANDUPDATEACITEMS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_ADDANDUPDATEBANKACCOUNT_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_DELETEACITEMS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_DELETEBANKACCOUNT_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_GETACITEMSBYFILTER_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_GETACITEMSBYSYSNOS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_GETBANKACCOUNTBYFILTER_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTAO_GETBANKACCOUNTBYSYSNOS_REQ_CMD_ID] = true;
}

////////////////////////
// class CACAccountAoService
CACAccountAoService::CACAccountAoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    enum DeployMethod method = DEPLOY_DETACH;
    if(!g_sacaccountaoStandAlone.empty() && g_sacaccountaoStandAlone != "0") // Stand-Alone
        method = DEPLOY_STANDALONE;
        
    m_pSvc = new CACAccountAo(method);
}

void CACAccountAoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CACAccountAoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CACAccountAoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case ACACCOUNTAO_ADDANDUPDATEACITEMS_REQ_CMD_ID:
        return AddAndUpdateACItems(oCmd);
	case ACACCOUNTAO_ADDANDUPDATEBANKACCOUNT_REQ_CMD_ID:
        return AddAndUpdateBankAccount(oCmd);
	case ACACCOUNTAO_DELETEACITEMS_REQ_CMD_ID:
        return DeleteACItems(oCmd);
	case ACACCOUNTAO_DELETEBANKACCOUNT_REQ_CMD_ID:
        return DeleteBankAccount(oCmd);
	case ACACCOUNTAO_GETACITEMSBYFILTER_REQ_CMD_ID:
        return GetACItemsByFilter(oCmd);
	case ACACCOUNTAO_GETACITEMSBYSYSNOS_REQ_CMD_ID:
        return GetACItemsBySysNos(oCmd);
	case ACACCOUNTAO_GETBANKACCOUNTBYFILTER_REQ_CMD_ID:
        return GetBankAccountByFilter(oCmd);
	case ACACCOUNTAO_GETBANKACCOUNTBYSYSNOS_REQ_CMD_ID:
        return GetBankAccountBySysNos(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CACAccountAoService::AddAndUpdateACItems(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoAddAndUpdateACItemsReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoAddAndUpdateACItemsResp oResp;
	
    oResp.dwResult = m_pSvc->AddAndUpdateACItems(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.aCItemsPo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aCItemsSysNo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aCItemsSysNo = oReq.aCItemsSysNo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::AddAndUpdateBankAccount(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoAddAndUpdateBankAccountReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoAddAndUpdateBankAccountResp oResp;
	
    oResp.dwResult = m_pSvc->AddAndUpdateBankAccount(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccount,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccountSysNo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccountSysNo = oReq.bankAccountSysNo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::DeleteACItems(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoDeleteACItemsReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoDeleteACItemsResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteACItems(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.ACItemsSysNo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::DeleteBankAccount(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoDeleteBankAccountReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoDeleteBankAccountResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteBankAccount(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountSysNo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::GetACItemsByFilter(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoGetACItemsByFilterReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoGetACItemsByFilterResp oResp;
	
    oResp.dwResult = m_pSvc->GetACItemsByFilter(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountFilter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aCItems,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aCItems = oReq.aCItems;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::GetACItemsBySysNos(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoGetACItemsBySysNosReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoGetACItemsBySysNosResp oResp;
	
    oResp.dwResult = m_pSvc->GetACItemsBySysNos(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.sysNoList,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aACItems,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aACItems = oReq.aACItems;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::GetBankAccountByFilter(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoGetBankAccountByFilterReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoGetBankAccountByFilterResp oResp;
	
    oResp.dwResult = m_pSvc->GetBankAccountByFilter(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountFilter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccount,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccount = oReq.bankAccount;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountAoService::GetBankAccountBySysNos(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountAoGetBankAccountBySysNosReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountAoGetBankAccountBySysNosResp oResp;
	
    oResp.dwResult = m_pSvc->GetBankAccountBySysNos(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.sysNoList,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccount,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccount = oReq.bankAccount;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
